package com.zryc.shop.oms.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.jwt.JWTUtil;
import com.zryc.shop.oms.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@RestController
@Api(tags = "订单管理接口列表")
@RequestMapping("/oms/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public Result list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页显示条数", required = true) Integer pageSize,
                       @RequestParam(required = false) @ApiParam("订单编号") String orderSn,
                       @RequestParam(required = false) @ApiParam("收货人信息：姓名/电话") String receiverKeyword,
                       @RequestParam(required = false) @ApiParam("订单状态") Integer status,
                       @RequestParam(required = false) @ApiParam("订单类型：正常/秒杀") Integer orderType,
                       @RequestParam(required = false) @ApiParam("订单来源：PC/App") Integer sourceType,
                       @RequestParam(required = false) @ApiParam("订单发起日期") @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime) {
        return Result.ok(orderService.list(pageNum, pageSize, orderSn, receiverKeyword, status, orderType, sourceType, createTime));
    }

    @PostMapping("/delete")
    @ApiOperation("删除订单")
    @Transactional
    public Result delete(@RequestParam String ids, @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.deleteOrder(ids, token));
    }

    @PostMapping("/update/close")
    @ApiOperation("关闭订单")
    @Transactional
    public Result close(@RequestParam @ApiParam("订单ID列表") String ids,
                        @RequestParam @ApiParam("操作日志备注") String note,
                        @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.close(ids, note, token));
    }

    @PostMapping("/update/receiverInfo")
    @ApiOperation("修改收件人信息")
    @Transactional
    public Result updateReceiverInfo(@RequestBody HashMap params, @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.updateReceiverInfo(params,token));
    }


    @PostMapping("/update/moneyInfo")
    @ApiOperation("修改订单费用信息")
    @Transactional
    public Result updateMoneyInfo(@RequestBody HashMap params, @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.updateMoneyInfo(params,token));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询订单详情")
    public Result info(@PathVariable Long id) {
        return Result.ok(orderService.detail(id));
    }

    @PostMapping("/update/note")
    @ApiOperation("修改备注信息")
    @Transactional
    public Result updateNote(@RequestParam @ApiParam(value = "订单ID", required = true) Long id,
                             @RequestParam @ApiParam(value = "订单状态", required = true) Integer status,
                             @RequestParam @ApiParam(value = "订单备注", required = true) String note,
                             @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.updateNode(id, status, note,token));
    }

    @PostMapping("/update/delivery")
    @ApiOperation("订单发货")
    public Result delivery(@RequestBody List<HashMap> params, @RequestHeader("Authorization") String token) {
        return Result.ok(orderService.delivery(params,token));
    }


}
